﻿using System;
using fastCSharp.emit;
using fastCSharp.reflection;
using System.Collections.Generic;

namespace fastCSharp.code
{
    /// <summary>
    /// 数据库表格模型
    /// </summary>
    internal partial class sqlModel
    {
        /// <summary>
        /// 数据库表格模型码生成
        /// </summary
        [auto(Name = "数据库表格模型", DependType = typeof(coder.cSharper), IsAuto = true)]
        internal sealed partial class cSharp : dataModel.cSharp<fastCSharp.code.cSharp.sqlModel>
        {
            /// <summary>
            /// 日志流字段
            /// </summary>
            private struct sqlStreamMember
            {
                /// <summary>
                /// 成员信息
                /// </summary>
                public memberInfo Member;
                /// <summary>
                /// 数据库成员信息
                /// </summary>
                public fastCSharp.emit.dataMember Attribute;
                /// <summary>
                /// 成员位图名称
                /// </summary>
                public string MemberMapName
                {
                    get { return "_m" + Member.MemberIndex.toString(); }
                }
                /// <summary>
                /// 成员加载函数名称
                /// </summary>
                public string MemberLoadedMethodName
                {
                    get { return Member.MemberName + "Loaded"; }
                }
            }
            /// <summary>
            /// 日志流字段
            /// </summary>
            private sqlStreamMember[] sqlStreamMembers;
            /// <summary>
            /// SQL表格操作工具 字段名称
            /// </summary>
            public string SqlTableName
            {
                get { return fastCSharp.emit.sqlTable.SqlTableName; }
            }
            /// <summary>
            /// SQL表格操作日志 字段名称
            /// </summary>
            public string SqlStreamName
            {
                get { return fastCSharp.emit.sqlTable.SqlStreamName; }
            }
            /// <summary>
            /// SQL表格计算列日志 字段名称
            /// </summary>
            public string SqlStreamLoadName
            {
                get { return fastCSharp.emit.sqlTable.SqlStreamLoadName; }
            }
            /// <summary>
            /// SQL表格计算列计数 字段名称
            /// </summary>
            public string SqlStreamCountName
            {
                get { return fastCSharp.emit.sqlTable.SqlStreamCountName; }
            }
            /// <summary>
            /// 安装下一个类型
            /// </summary>
            protected override void nextCreate()
            {
                sqlStreamMember[] members = code.memberInfo.GetMembers(type, Attribute.MemberFilter)
                        .getArray(value => new sqlStreamMember { Member = value, Attribute = value.GetAttribute<fastCSharp.emit.dataMember>(Attribute.IsBaseTypeAttribute, Attribute.IsInheritAttribute) });
                if (Attribute.LogTcpCallService != null)
                {
                    sqlStreamMembers = members.getFindArray(value => value.Attribute != null && value.Attribute.IsIgnoreCurrent && value.Attribute.IsSqlStream);
                    if (Attribute.IsLogMemberMap)
                    {
                        fastCSharp.emit.dataSerialize dataSerialize = type.Type.customAttribute<fastCSharp.emit.dataSerialize>(true);
                        if (dataSerialize != null && !dataSerialize.IsMemberMap) error.Message("数据库日志流处理类型 " + type.FullName + " 序列化不支持成员位图");
                    }
                }
                subArray<sqlStreamMember> strings = members.getFind(value => value.Member.Type == typeof(string) && (value.Attribute == null || (value.Attribute.MaxStringLength == 0 && !value.Attribute.IsIgnoreMaxStringLength)));
                if (strings.Count != 0)
                {
                    error.Message(type.FullName + " 字符串字段缺少最大长度限制 " + strings.joinString(',', value => value.Member.MemberName));
                }
                base.nextCreate();
            }
        }
    }
}